package com.jsm.tf.net.invoke;

import com.alibaba.fastjson.JSON;
import com.jsm.tf.net.NettyClient;
import com.jsm.tf.net.command.Command;
import com.jsm.tf.net.command.CommandFactory;
import com.jsm.tf.net.message.Message;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


@Component
@Slf4j
public class CommandInvoker {
    @Resource
    private CommandFactory commandFactory;
    @Resource
    private NettyClient nettyClient;

    public void invoke(Message message, Channel channel) {
        Command command = commandFactory.build(message.getClass());
        nettyClient.ackSyncMsg(message);
        if (command != null) {
            command.execute(message, channel);
        } else {
            log.error("message:{}  class:{},对应命令操作不存在", JSON.toJSONString(message), message.getClass());
        }
    }
}
